﻿using DMC.Core.Models.Dto;
using DMC.Core.Models.Queries;
using DMC.Core.Providers;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Platform.Exceptions;

namespace DMC.Core.Services.Impls
{
    [AutoResolve]
    public class ConnectionService : Component, IConnectionService
    {
        public ConnectionService()
        {
        }

        [AutoWired]
        protected IConnectionProvider Provider { get; set; }

        public ResultObject<IEnumerable<ConnectionDto>> Query(QueryObject<ConnectionQuery> request)
        {
            return Provider.Query(request);
        }

        public ConnectionDto Create(ConnectionDto connection)
        {
            if (Provider.Create(connection)) return connection;
            return null;
        }

        public ConnectionDto Update(ConnectionDto connection)
        {
            if (Provider.Update(connection)) return connection;
            return null;
        }

        public void Delete(ConnectionDto connection)
        {
            if (!Provider.Delete(connection))
                throw new PlatformException("ERR_OperateFail");
        }

        public ResultObject<object[]> GetDbProviders()
        {
            return Provider.GetDbProviders();
        }

        public void Sync()
        {
            Provider.Sync();
        }

        public void Test(ConnectionTestDto connectionTestDto)
        {
            Provider.Test(connectionTestDto.Provider, connectionTestDto.ConnectionString);
        }

    }
}
